package sdk.webview.fmc.com.fmcsdk.check.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.check.adapter.ImagePagerAdapter;
import sdk.webview.fmc.com.fmcsdk.check.adapter.ReadonlyWorkDetailAdapter;
import sdk.webview.fmc.com.fmcsdk.check.bean.GetDetailsBean;
import sdk.webview.fmc.com.fmcsdk.check.bean.ImageSelectBean;
import sdk.webview.fmc.com.fmcsdk.check.dialog.CenterPopupWindow;
import sdk.webview.fmc.com.fmcsdk.check.interfaces.ReadonlyWorkDetailView;
import sdk.webview.fmc.com.fmcsdk.check.persenter.ReadonlyWorkDetailPresenter;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.GsonUtil;
import sdk.webview.fmc.com.fmcsdk.util.StringUtils;
import sdk.webview.fmc.com.fmcsdk.util.ToastUtils;

public class ReadonlyWorkDetailActivity extends BaseActivity<ReadonlyWorkDetailView, ReadonlyWorkDetailPresenter> implements ReadonlyWorkDetailView{
    private ImageView iv_back;
    private TextView tv_title;
    private RecyclerView rv_data;

    private LinearLayout ll_content;

    private String pointData;

    private List<ImageSelectBean> recordList = new ArrayList<>();
    private ReadonlyWorkDetailAdapter detailAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_readonly_work_detail;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        // 在 Activity 的 onCreate() 方法中设置状态栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setStatusBarColor(ContextCompat.getColor(this, R.color.color_DC3C26)); // 设置状态栏颜色
        }

        initView();
        initRecycler();
        initData();
        initListener();
    }

    @Override
    protected ReadonlyWorkDetailPresenter createPresenter() {
        return new ReadonlyWorkDetailPresenter();
    }

    private void initView(){
        iv_back = findViewById(R.id.iv_back);
        tv_title = findViewById(R.id.tv_title);
        rv_data = findViewById(R.id.rv_data);
        ll_content = findViewById(R.id.ll_content);
    }

    private void initRecycler(){
        rv_data.setLayoutManager(new LinearLayoutManager(context));
        detailAdapter = new ReadonlyWorkDetailAdapter(R.layout.item_readonly_work_detail, (position, list) -> {
            if(list.get(position).getImg().contains("mp4")){
                Intent intent = new Intent(context, VideoPlaybackActivity.class);
                intent.putExtra("videoUrl", list.get(position).getImg());
                startActivity(intent);
            }else{
                showCenterPopupWindow(ll_content, position, list);
            }


        });
        rv_data.setAdapter(detailAdapter);
        detailAdapter.bindToRecyclerView(rv_data);
        detailAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.ll_shui){
                GetDetailsBean.RecordDTO.PointsDTO.CheckListDTO adapterItem = detailAdapter.getItem(position);
                Intent intent = new Intent(context, ReadonlyWaterMeterActivity.class);
                intent.putExtra("item",GsonUtil.BeanToJson(adapterItem));
                startActivity(intent);
            }
        });

    }

    private void initData(){
        pointData = getIntent().getStringExtra("pointData");
        if (!StringUtils.isEmpty(pointData)){
            GetDetailsBean.RecordDTO.PointsDTO pointsDTO = GsonUtil.GsonToBean(pointData, GetDetailsBean.RecordDTO.PointsDTO.class);
          for (int i=0;i<pointsDTO.getCheckList().size();i++){
            GetDetailsBean.RecordDTO.PointsDTO.CheckListDTO item=  pointsDTO.getCheckList().get(i);
              List<ImageSelectBean> imageList = pointsDTO.getCheckList().get(i).getImageListShow();
              if (!item.getPictureUrls().isEmpty()){
                  for (GetDetailsBean.RecordDTO.PointsDTO.CheckListDTO.PictureUrlsDTO pictureUrl : item.getPictureUrls()) {
                      ImageSelectBean imageSelectBean;
                      if (pictureUrl.getUrl().contains("mp4")){
                          if (!pictureUrl.getUrl().contains("http")){
                              String baseUrl = PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.BASE_ADDRESS_URL, "");
                              imageSelectBean = new ImageSelectBean(baseUrl+pictureUrl.getUrl(),2);
                              imageList.add(imageSelectBean);
                          }


                      }else {
                          if (!pictureUrl.getUrl().contains("http")){
                              String baseUrl = PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.BASE_ADDRESS_URL, "");
                              imageSelectBean = new ImageSelectBean(baseUrl+pictureUrl.getUrl(),1);
                              imageList.add(imageSelectBean);
                          }

                      }
                  }
              }else {
                  imageList.clear();
              }
              item.setImageListShow(imageList);

          }

            detailAdapter .setNewData(pointsDTO.getCheckList());
            tv_title.setText(StringUtils.isEmpty(pointsDTO.getAssetDesc()) ? pointsDTO.getLocationDesc() : pointsDTO.getAssetDesc());
        }

    }


    private void initListener(){
        iv_back.setOnClickListener(v ->{
            finish();
        });
    }

    private void showCenterPopupWindow(View anchor, int position, List<ImageSelectBean> list) {
        ImageSelectBean selectBean=list.get(position);
        recordList.clear();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getImg().contains("mp4") || list.get(i).getImg().contains("MP4") || TextUtils.isEmpty(list.get(i).getImg())) {
                continue;
            }
            recordList.add(list.get(i));
        }
        CenterPopupWindow centerPopupWindow = new CenterPopupWindow(context, R.layout.dialog_center_img);
        ViewPager viewPager = centerPopupWindow.getContentView().findViewById(R.id.viewPager);
        ImageView iv_left = centerPopupWindow.getContentView().findViewById(R.id.iv_left);
        ImageView iv_right = centerPopupWindow.getContentView().findViewById(R.id.iv_right);
        iv_left.setOnClickListener(v -> {
            int currentItem = viewPager.getCurrentItem();
            if (currentItem > 0) {
                viewPager.setCurrentItem(currentItem - 1, true); // true for smooth scroll
            }
        });
        iv_right.setOnClickListener(v -> {
            int currentItem = viewPager.getCurrentItem();
            int totalItems = viewPager.getAdapter().getCount();
            if (currentItem < totalItems - 1) {
                viewPager.setCurrentItem(currentItem + 1, true); // true for smooth scroll
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (recordList.size() == 1) {
                    iv_left.setVisibility(View.GONE);
                    iv_right.setVisibility(View.GONE);
                } else {
                    if (i <= 0) {
                        iv_left.setVisibility(View.GONE);
                        iv_right.setVisibility(View.VISIBLE);
                    } else if (i >= recordList.size() - 1) {
                        iv_left.setVisibility(View.VISIBLE);
                        iv_right.setVisibility(View.GONE);
                    } else {
                        iv_left.setVisibility(View.VISIBLE);
                        iv_right.setVisibility(View.VISIBLE);
                    }
                }
            }


            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        centerPopupWindow.getContentView().findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                centerPopupWindow.dismiss();
            }
        });

        ImagePagerAdapter pagerAdapter = new ImagePagerAdapter(context, recordList);
        viewPager.setAdapter(pagerAdapter);

        int positionSelect = 0;
        for (int i = 0; i < recordList.size(); i++) {
            if (TextUtils.equals(selectBean.getImg(), recordList.get(i).getImg())) {
                positionSelect = i;
            }
        }
        if (positionSelect == 1) {
            iv_left.setVisibility(View.GONE);
            iv_right.setVisibility(View.VISIBLE);
        }
        if (recordList.size() == 1) {
            iv_left.setVisibility(View.GONE);
            iv_right.setVisibility(View.GONE);
        } else {
            if (positionSelect == 0) {
                iv_left.setVisibility(View.GONE);
                iv_right.setVisibility(View.VISIBLE);
            } else if (positionSelect == recordList.size() - 1) {
                iv_left.setVisibility(View.VISIBLE);
                iv_right.setVisibility(View.GONE);
            }else {
                iv_left.setVisibility(View.VISIBLE);
                iv_right.setVisibility(View.VISIBLE);
            }
        }


        viewPager.setCurrentItem(positionSelect);
        centerPopupWindow.show(anchor);
    }

    private void showCenterPopupWindowPlayVideo(View anchor,String  videoUrl) {
        CenterPopupWindow centerPopupWindow = new CenterPopupWindow(context, R.layout.layout_center_video_view);
        VideoView videoView=centerPopupWindow.getContentView().findViewById(R.id.video_view);
        Uri uri = Uri.parse(videoUrl);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
        MediaController mediaController = new MediaController(context);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.start();
        centerPopupWindow.getContentView().findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                centerPopupWindow.dismiss();
            }
        });

        centerPopupWindow.show(anchor);
    }

}
